<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class MailBoxPage {

	private $alerter;
	private $mailer;
	private $toolbox;
	private $db_access;
	private $access;
	
	public function __construct() {
		$this->alerter = new Alerter();
		$this->mailer = new Mailer();
		$this->toolbox = new Toolbox();
		$this->db_access = new DbAccessor();
		$this->access = new AccessManager();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		$this->access->check_access("READ");
		if (!$_SESSION['user_id']) {
			errorpage(6);
		}
	}

	function page_inc_execute_action($page, $action) {

		switch($action) {
			default:
				$this->alerter->add_alert("unknown action: $action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		
		global $CONFIG;
		$CONTENT = array();
		$THISDIR = $page->directory_tree[$page->dir_depth-1];
		
		// take care of mailform
		$this->toolbox->add_action($page, 'mail', 'show', 'mf');
		if ($_GET['mailto']) {
			$page->mailform = $this->mailer->mail_form(array('to' => $_GET['mailto']));
		}

		// take care of mails in progress
		if ($_POST['submitted'] and $_POST['mailing']) {
			$mail = new InternalMail();
			$mail->set_to_keys($_POST['to']);
			$mail->set_cc_keys($_POST['cc']);
			$mail->body = $_POST['body'];
			$mail->subject = $_POST['subject'];
			$mail->icon = $_POST['icon'];
			$mail->from = $_SESSION['user_id'];
			$mail->send_mail();
		}

		// mailbox browsing actions
		if (!$_GET['out'] and !$_GET['trash']) { // inbox
			$CONTENT['title'] = "INBOX: {$_SESSION['member']['nick']}";
			$this->toolbox->add_action($page, 'outbox', 'url', '.mailbox?out=1');
			$this->toolbox->add_action($page, 'inbox-t', 'url', '.mailbox?trash=1');
			$this->toolbox->add_action($page, 'outbox-t', 'url', '.mailbox?out=1&trash=1');
		} else if (!$_GET['out'] and $_GET['trash']) { // inbox > trash
			$CONTENT['title'] = "INBOX [prullenbak]: {$_SESSION['member']['nick']}";
			$this->toolbox->add_action($page, 'inbox', 'url', '.mailbox');
			$this->toolbox->add_action($page, 'outbox', 'url', '.mailbox?out=1');
			$this->toolbox->add_action($page, 'outbox-t', 'url', '.mailbox?out=1&trash=1');
		} else if ($_GET['out'] and !$_GET['trash']) { // outbox
			$CONTENT['title'] = "OUTBOX: {$_SESSION['member']['nick']}";
			$this->toolbox->add_action($page, 'inbox', 'url', '.mailbox');
			$this->toolbox->add_action($page, 'inbox-t', 'url', '.mailbox?trash=1');
			$this->toolbox->add_action($page, 'outbox-t', 'url', '.mailbox?out=1&trash=1');
		} else if ($_GET['out'] and $_GET['trash']) { // outbox > trashed
			$CONTENT['title'] = "OUTBOX [prullenbak]: {$_SESSION['member']['nick']}";
			$this->toolbox->add_action($page, 'inbox', 'url', '.mailbox');
			$this->toolbox->add_action($page, 'inbox-t', 'url', '.mailbox?trash=1');
			$this->toolbox->add_action($page, 'outbox', 'url', '.mailbox?out=1');
		}

		// take care of actions
		if ($_POST['action'] and $_POST['target']) {

			if (!ereg("^([0-9]+,)*[0-9]+$", $_POST['target'])) {
				errorpage(9);
			}

			// check access
			if (!$_GET['out']) { // inbox
				$sql = $this->db_access->db_query_select(
						array(array(), "MAIL_HEADER.mail_id"), 
						array("MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id", "DUMMY"), 
						"MAIL_TO.user_id = {$_SESSION['user_id']} AND !MAIL_TO.deleted AND MAIL_HEADER.mail_id IN ({$_POST['target']})");
			} else {
				$sql = $this->db_access->db_query_select(
						array(array("mail_id")), array("MAIL_HEADER"), "MAIL_HEADER.sender = {$_SESSION['user_id']} AND !MAIL_HEADER.deleted AND MAIL_HEADER.mail_id IN ({$_POST['target']})");
			}

			if ($this->db_access->db_num_rows(db_do_query($page, $sql)) != (substr_count($_POST['target'], ",") + 1)) {
				errorpage(10);
			}


			// process action
			switch($_POST['action']) {
				case "delete":
					if (!$_GET['out']) { // inbox
						$this->db_access->db_do_query($this->db_access->db_query_update(array('deleted'), array(1), "MAIL_TO", "mail_id IN ({$_POST['target']})"));	
					} else { // outbox
						$this->db_access->db_do_query($this->db_access->db_query_update(array('sent', 'deleted'), array('sent', 1), "MAIL_HEADER", "mail_id IN ({$_POST['target']})"));
					}
				break;
				case "trash":
					case "untrash":
					if (!$_GET['out']) {// inbox
						$this->db_access->db_do_query($this->db_access->db_query_update(array('trashed'), array(($_POST['action'] == "trash") ? 1 : 0), "MAIL_TO", "mail_id IN ({$_POST['target']})"));	
					} else {// outbox
						$this->db_access->db_do_query($this->db_access->db_query_update(array('sent', 'trashed'), array('sent', ($_POST['action'] == "trash") ? 1 : 0), "MAIL_HEADER", "mail_id IN ({$_POST['target']})"));
					}
				break;
				case "markread": //TODO: remove fall-through
					case "markunread":
					if (!$_GET['out']) { // inbox
						$this->db_access->db_do_query($this->db_access->db_query_update(array('hasread'), array(($_POST['action'] == "markread") ? 1 : 0), "MAIL_TO", "mail_id IN ({$_POST['target']})"));	
					} else { // outbox
						$this->db_access->db_do_query($this->db_access->db_query_update(array('sent', 'hasread'), array('sent', ($_POST['action'] == "markread") ? 1 : 0), "MAIL_HEADER", "mail_id IN ({$_POST['target']})"));
					}
				break;
				default:
				break;
			}
		}

		// list mails
		if (!$_GET['out'] and !$_GET['trash']) { // inbox
			$sql = $this->db_access->db_query_select(array(array(), "MAIL_HEADER.sent, MAIL_HEADER.subject, MAIL_HEADER.sender AS corr_id, MAIL_TO.*, MEMBER.nick"), array("(MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id) INNER JOIN MEMBER ON MAIL_HEADER.sender = MEMBER.user_id", "DUMMY"), "MAIL_TO.user_id = {$_SESSION['user_id']} AND !MAIL_TO.trashed AND !MAIL_TO.deleted", "sent ASC");
		} else if (!$_GET['out'] and $_GET['trash']) { // inbox > trash
			$sql = $this->db_access->db_query_select(array(array(), "MAIL_HEADER.sent, MAIL_HEADER.subject, MAIL_HEADER.sender AS corr_id, MAIL_TO.*, MEMBER.nick"), array("(MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id) INNER JOIN MEMBER ON MAIL_HEADER.sender = MEMBER.user_id", "DUMMY"), "MAIL_TO.user_id = {$_SESSION['user_id']} AND MAIL_TO.trashed AND !MAIL_TO.deleted", "sent ASC");
		} else if ($_GET['out'] and !$_GET['trash']) { // outbox
			$sql = $this->db_access->db_query_select(array(array(), "MAIL_HEADER.*, MAIL_TO.user_id, MAIL_TO.cc, MEMBER.nick"), array("(MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id) INNER JOIN MEMBER ON MAIL_TO.user_id = MEMBER.user_id", "DUMMY"), "MAIL_HEADER.sender = {$_SESSION['user_id']} AND !MAIL_HEADER.trashed AND !MAIL_HEADER.deleted", "sent ASC, cc ASC");
		} else if ($_GET['out'] and $_GET['trash']) { // outbox > trashed
			$sql = $this->db_access->db_query_select(array(array(), "MAIL_HEADER.*, MAIL_TO.user_id, MAIL_TO.cc, MEMBER.nick"), array("(MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id) INNER JOIN MEMBER ON MAIL_TO.user_id = MEMBER.user_id", "DUMMY"), "MAIL_HEADER.sender = {$_SESSION['user_id']} AND MAIL_HEADER.trashed AND !MAIL_HEADER.deleted", "sent ASC, cc ASC");
		}

		$sqlresult = $this->db_access->db_do_query($sql);

		$mails = array();
		while ($m = $this->db_access->db_fetch_array($sqlresult)) {
			if ($m['mail_id'] != $prev_m['mail_id']) {
				array_unshift($mails, $prev_m);
				$m['new'] = (($m['sent'] > $_SESSION['member']['last_visit']) and !$m['hasread']) ? 1 : 0;
				$m['nick'] = array($m['nick']);
				$m['corr_id'] = array($m['corr_id']);
				$prev_m = $m;
			} else {
				$prev_m['nick'][] = $m['nick'];
				$prev_m['corr_id'][] = $m['corr_id'];	
			}
		}
		array_unshift($mails, $prev_m);

		$CONTENT['mails'] = $mails;

		// add actions
		if ($this->db_access->db_num_rows($sqlresult) > 0) {
			$this->toolbox->add_action($page, "mails-markall", "javascript", "", "mark_all();");
			$this->toolbox->add_action($page, "mails-markread", "javascript", "", "selected_markread();");
			$this->toolbox->add_action($page, "mails-markunread", "javascript", "", "selected_markunread();");
			if (!$_GET['trash']) {
				$this->toolbox->add_action($page, "mails-trash", "javascript", "", "selected_trash();");
			} else {
				$this->toolbox->add_action($page, "mails-untrash", "javascript", "", "selected_untrash();");
			}
			$this->toolbox->add_action($page, "mails-delete", "javascript", "", "selected_delete();");
		}

		$page->content = $CONTENT;

	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_maillist($page, $page->content['mails'], $_GET['out'], $_GET['trash']);	
	}
}

?>
